package com.zwj.batch.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author zwj
 * @Description: 第三方PC端登录同意返回结果
 * @date 2022/10/12 2:17 下午
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;

    protected static <T> Result<T> build(T data) {
        Result<T> result = new Result<T>();
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    /**
     * 构建返回结果集
     * @param body
     * @param resultCodeEnum
     * @return
     * @param <T>
     */
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMsg());
        return result;
    }

    /**
     * 操作成功，有返回结果值
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    /**
     * 操作成功，无返回结果值
     *
     * @param <T>
     * @return
     */
    public static Result<String> ok() {
        return build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 操作失败
     * @param resultCodeEnum
     * @return
     */
    public static Result fail(ResultCodeEnum resultCodeEnum) {
        return build(null, resultCodeEnum);
    }
}
